/**
 * Created by aifece on 15/1/22.
 */
(function(){
  var ActLo = function(config)
  {
    this.default = {
      P_x_0: 0,
      P_y_0: 0,
      P_x_1: 0,
      P_y_1: 0,
      time : 1000,
      image: siteUrl+'/images/logo.png',
      actImg : '.actLo-image',
    };
    this.options = $.extend(this.default, config);

    this._init();
  }

  ActLo.prototype._init = function () {
    var _this = this;
    this.createDiv();
    $('.actLo-image').css({position: 'absolute', left: this.options.P_x_0 + 'px', top: this.options.P_y_0 + 'px'});

    this.mathPath();

    var pathArr = this.mathPath();

    var tmpI = 0;
    var dong = setInterval(function(){
      $('.actLo-image').css({left: pathArr[tmpI][0] + 'px', top: pathArr[tmpI][1] + 'px'});
      if (tmpI+1 == pathArr.length || pathArr[tmpI][1] < _this.options.P_y_1) {
        clearInterval(dong);
        $('.actLo-image').remove();
        $('.operation .buy-car').parent().addClass('curr');
        setTimeout(function(){
          $('.operation .buy-car').parent().removeClass('curr');
        }, 500);
      }
      tmpI++;
    }, 10);

  }

  ActLo.prototype.createDiv = function () {
    var html = '<div class="actLo-image"><img src="' + this.options.image + '" width="100"/></div>';
    $('body').append(html);
  }

  ActLo.prototype.mathPath = function () {
    var tmpPath = new Array();
    var X = Math.abs(this.options.P_x_1 - this.options.P_x_0);
    var Y = Math.abs(this.options.P_y_1 - this.options.P_y_0);

    var minY = Y/5*2;
    var minTime = this.options.time/50*2;
    var stepX = X/5*2/minTime;
    var tmpStep = minY/8;
    var tmpY = this.options.P_y_0;
    var tmpX = this.options.P_x_0;

    for (var i = 0; i < minTime; i++) {
      tmpStep = tmpStep-tmpStep/8;
      tmpY = tmpY+tmpStep;
      tmpX = tmpX+stepX;
      tmpPath.push([tmpX, tmpY]);
    }

    var endTime = this.options.time/50*3;
    for (var i = 0; i < endTime; i++) {
      tmpStep = tmpStep+tmpStep/8;
      tmpY = tmpY-tmpStep;
      tmpX = tmpX+stepX;
      tmpPath.push([tmpX, tmpY]);
    }

    return tmpPath;
  }

  window.ActLo = ActLo;
})();